package com.kwq.gateway.config;


import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 配置了一个id为routr-name的路由规则
 * 当访问地址http://localhost:9527/guonei时会自动转发到http://news.baidu.com/guonei
 * */
@Configuration
public class GateWayConfig
{
//    @Bean
//    public RouteLocator customRouteLocator(RouteLocatorBuilder routeLocatorBuilder){
//        RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();
//        routes.route("test_route_incode",r -> r.path("/guonei").uri("http://news.baidu.com/guonei")).build();
//        return routes.build();
//    }

    /**
     * webflux写法
     * @param builder
     * @return
     */
    @Bean
    public RouteLocator routeLocator(RouteLocatorBuilder builder) {
        return builder.routes()
                .route(
                        p ->
                        p.path("/guonei")			//请求路径
                         .uri("http://news.baidu.com/guonei")			//指向的uri
                         .id("test_route_incode")
                )
                .route("test_route",
                        p ->
                        p.path("/blog")
                         .uri("http://weiqk.xyz")
                )
                .build();
    }

}